﻿/*******************************************************
* Copyright (c) 2018-2088, By XuXiang all rights reserved.
*
* FileName: CSVLoader.cpp
* Summary: CSV文件加载。
*
* Author: XuXiang
* Date: 2024-05-09 23:32
*******************************************************/

#include "CSVLoader.h"
#include <iostream>
#include <fstream>
#include <assert.h>
#include "../Util/StringUtil.h"

namespace Competition
{
	CSVLoader::CSVLoader()
	{
	}

	CSVLoader::~CSVLoader()
	{
	}

	void CSVLoader::Init(const string& file)
	{
		fstream fs;
		fs.open(file, ios::in);
		if (!fs.is_open())
		{
			cout << "打开配置文件失败:" << file << endl;
			return;
		}

		//读取表头
		string line;
		getline(fs, line);
		vector<string> fields;
		StringUtil::Split(line, "\t", fields);
		for (int i = 0; i < fields.size(); ++i)
		{
			m_FieldIndex.insert(make_pair(fields[i], i));
		}
		getline(fs, line);

		//读取数据内容
		while (m_DataLines.size() > 0) { m_DataLines.pop(); }
		while (getline(fs, line))
		{
			m_DataLines.push(line);
		}
		fs.close();
	}

	bool CSVLoader::ReadNextRecord()
	{
		if (m_DataLines.empty())
		{
			return false;
		}

		string line = m_DataLines.front();
		m_DataLines.pop();
		m_CurValues.clear();
		StringUtil::Split(line, "\t", m_CurValues);
		return true;
	}

	bool CSVLoader::ReadBool(const string& field)
	{
		auto itr = m_FieldIndex.find(field);
		if (itr != m_FieldIndex.end())
		{
			string& str_v = m_CurValues[itr->second];
			return !(str_v.empty() || str_v.compare("0") == 0 || str_v.compare("false") == 0);
		}
        return false;
	}

	void CSVLoader::ReadBoolArray(const string& field, vector<bool> &v)
	{
		auto itr = m_FieldIndex.find(field);
		v.clear();
		if (itr != m_FieldIndex.end())
		{
			string& str_v = m_CurValues[itr->second];
			vector<string> str_split;
			StringUtil::Split(str_v, "|", str_split);
			for (vector<string>::iterator itr = str_split.begin(); itr != str_split.end(); ++itr)
			{
				bool b = !(itr->empty() || itr->compare("0") == 0 || itr->compare("false") == 0);
				v.push_back(b);
			}
		}
	}

	int CSVLoader::ReadInt(const string& field)
	{
		auto itr = m_FieldIndex.find(field);
		if (itr != m_FieldIndex.end())
		{
			string& str_v = m_CurValues[itr->second];
			return ::atoi(str_v.c_str());
		}
        return 0;
	}

	void CSVLoader::ReadIntArray(const string& field, vector<int> &v)
	{
		auto itr = m_FieldIndex.find(field);
		v.clear();
		if (itr != m_FieldIndex.end())
		{
			string& str_v = m_CurValues[itr->second];
			StringUtil::SplitToInt(str_v, "|", v);
		}
	}

	float CSVLoader::ReadFloat(const string& field)
	{
		auto itr = m_FieldIndex.find(field);
		if (itr != m_FieldIndex.end())
		{
			string& str_v = m_CurValues[itr->second];
			return (float)::atof(str_v.c_str());
		}
		return 0;
	}

	void CSVLoader::ReadFloatArray(const string& field, vector<float> &v)
	{
		auto itr = m_FieldIndex.find(field);
		v.clear();
		if (itr != m_FieldIndex.end())
		{
			string& str_v = m_CurValues[itr->second];
			StringUtil::SplitToFloat(str_v, "|", v);
		}
	}

	string CSVLoader::ReadString(const string& field)
	{
		auto itr = m_FieldIndex.find(field);
		if (itr != m_FieldIndex.end())
		{
			string& str_v = m_CurValues[itr->second];
			return str_v;
		}
        return string();
	}

	void CSVLoader::ReadStringArray(const string& field, vector<string> &v)
	{
		auto itr = m_FieldIndex.find(field);
		v.clear();
		if (itr != m_FieldIndex.end())
		{
			string& str_v = m_CurValues[itr->second];
			StringUtil::Split(str_v, "|", v);
		}
	}
}